#!/bin/bash

# make sure we are running as root
if [ $(id -u) != 0 ]; then echo "Sorry, must be root.  Exiting..."; exit 1; fi

mkdir -p /etc/aws

#Create credential file
if [ ! -f /etc/aws/auth.conf ]; then
cat << EOT > /etc/aws/auth.conf
AWSAccessKeyId=YOURACCESKEY
AWSSecretKey=YOURSECRETKEY
EOT
echo "please edit $AWS_CREDENTIAL_FILE"
chmod 600
fi


#Create environment file
if [ ! -f /etc/aws/env.conf ]; then
cat << EOT > /etc/aws/env.conf
JAVA_HOME=$( dirname $( dirname $( readlink -e /usr/bin/java ) ) )
AWS_CLOUDWATCH_HOME=/usr/local/lib/CloudWatch-1.0.13.4
AWS_CREDENTIAL_FILE=/etc/aws/auth.conf
AWS_SERVER=169.254.169.254
AWS_NAMESPACE="eCloud"
AWS_DIMENSIONS="MonitorPlus"
filesystemlist="/"
EOT
fi

#Instal binaries
install cw-write-*.sh /usr/local/bin

#Install  crontab
echo "*/5 * * * * root /usr/local/bin/cw-write-memory-metrics.sh" > /etc/cron.d/cw-write-memory-metrics
echo "*/5 * * * * root /usr/local/bin/cw-write-storage-used.sh" > /etc/cron.d/cw-write-storage-used